﻿using SWS;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Nav_Move : MonoBehaviour {


    private road_data[] data = null;


    public PathManager path;
    public HorizaontalPoint road;
    private Vector3[] road_data = null;

    public bool play_on_road = false;
    private int next_step = 0;
    private bool is_walking = false;
    private float walk_time = 0.0f;
    private float speed = 5.0f;
    private float passed_time = 0.0f;


    private void Start()
    {
        data = HorizaontalPoint.road_Data;
        //Vector3[] ctrl_points = path.GetPathPoints();
        //road_data = WaypointManager.GetCurved(ctrl_points);//根据几个坐标点来绘制路径坐标点
        if (play_on_road)
        {
            walk_on_road();
        }
    }

    void walk_on_road()
    {
        if (road_data.Length < 2)
            return;
        Debug.Log("进入walk");
        this.transform.position = this.road_data[0];
        next_step = 1;
        walk_to_next();
    }

    void walk_to_next()
    {
        if (next_step >= road_data.Length)
        {
            is_walking = false;
            return;
        }

        Vector3 src = this.transform.position;
        Vector3 dst = road_data[next_step];
        Vector3 dir = dst - src;
        float len = dir.magnitude;
        if (len <= 0)
        {
            next_step++;
            walk_to_next();
            return;
        }
        is_walking = true;
        walk_time = len / speed;
        passed_time = 0;
        this.transform.LookAt(dst);
    }

    private void Update()
    {
        if (!is_walking)
            return;

        float dt = Time.deltaTime;
        passed_time += dt;
        if (this.passed_time > this.walk_time)
        {
            dt -= (passed_time - walk_time);
        }

        float s = speed * dt;
        this.transform.Translate(this.transform.forward * s, Space.World);

        if(passed_time >= walk_time)
        {
            next_step++;
            walk_to_next();
        }
    }
}
